import { parseBoolean } from '../../../utils/utils';
import BaseXform from '../base-xform';

class ColXform extends BaseXform {
    get tag() {
        return 'col';
    }

    prepare(model, options) {
        const styleId = options.styles.addStyleModel(model.style || {});
        if (styleId) {
            model.styleId = styleId;
        }
    }

    render(xmlStream, model) {
        xmlStream.openNode('col');
        xmlStream.addAttribute('min', model.min);
        xmlStream.addAttribute('max', model.max);
        if (model.width) {
            xmlStream.addAttribute('width', model.width);
        }
        if (model.styleId) {
            xmlStream.addAttribute('style', model.styleId);
        }
        if (model.hidden) {
            xmlStream.addAttribute('hidden', '1');
        }
        if (model.bestFit) {
            xmlStream.addAttribute('bestFit', '1');
        }
        if (model.outlineLevel) {
            xmlStream.addAttribute('outlineLevel', model.outlineLevel);
        }
        if (model.collapsed) {
            xmlStream.addAttribute('collapsed', '1');
        }
        xmlStream.addAttribute('customWidth', '1');
        xmlStream.closeNode();
    }

    parseXmlObject(node) {
        if (node._name === 'col') {
            const model = (this.model = {
                min: parseInt(node._attributes.min || '0', 10),
                max: parseInt(node._attributes.max || '0', 10),
                width:
                node._attributes.width === undefined
                    ? undefined
                    : parseFloat(node._attributes.width || '0'),
            });
            if (node._attributes.style) {
                model.styleId = parseInt(node._attributes.style, 10);
            }
            if (parseBoolean(node._attributes.hidden)) {
                model.hidden = true;
            }
            if (parseBoolean(node._attributes.bestFit)) {
                model.bestFit = true;
            }
            if (node._attributes.outlineLevel) {
                model.outlineLevel = parseInt(node._attributes.outlineLevel, 10);
            }
            if (parseBoolean(node._attributes.collapsed)) {
                model.collapsed = true;
            }
            return true;
        }
        return false;
    }

    parseOpen(node) {
        if (node.name === 'col') {
            const model = (this.model = {
                min: parseInt(node.attributes.min || '0', 10),
                max: parseInt(node.attributes.max || '0', 10),
                width:
                node.attributes.width === undefined
                    ? undefined
                    : parseFloat(node.attributes.width || '0'),
            });
            if (node.attributes.style) {
                model.styleId = parseInt(node.attributes.style, 10);
            }
            if (parseBoolean(node.attributes.hidden)) {
                model.hidden = true;
            }
            if (parseBoolean(node.attributes.bestFit)) {
                model.bestFit = true;
            }
            if (node.attributes.outlineLevel) {
                model.outlineLevel = parseInt(node.attributes.outlineLevel, 10);
            }
            if (parseBoolean(node.attributes.collapsed)) {
                model.collapsed = true;
            }
            return true;
        }
        return false;
    }

    parseText() {
    }

    parseClose() {
        return false;
    }

    reconcile(model, options) {
        // reconcile column styles
        if (model.styleId) {
            model.style = options.styles.getStyleModel(model.styleId);
        }
    }
}

export default ColXform;
